package com.ice.body.business.service.impl;
import cn.hutool.core.date.DateUtil;
import com.ice.body.business.dao.ReagentOutMasterDao;
import com.ice.body.business.dao.ReagentStockDao;
import com.ice.body.business.model.ReagentOutDetailModel;
import com.ice.body.business.model.ReagentOutMasterModel;
import com.ice.body.business.model.ReagentStock;
import com.ice.body.business.service.ReagentOutDetailService;
import com.ice.body.business.service.ReagentOutMasterService;
import com.ice.base.dao.BaseDao;
import com.ice.base.service.impl.BaseServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * Created with GuoYanBin.
 * Description:业务接口-借出主表 serviceImpl
 * Date: 2021-06-22
 * Time: 03:20
 */
@Service
public class ReagentOutMasterServiceImpl extends BaseServiceImpl<ReagentOutMasterModel> implements ReagentOutMasterService{
    @Resource 
    private ReagentOutMasterDao dao;
    @Resource
    private ReagentStockDao reagentStockDao;
    @Resource
    private ReagentOutDetailService reagentOutDetailService;
    @Override
    public BaseDao<ReagentOutMasterModel> getBaseDao() {
        return dao;
    }



    @Override
    @Transactional
    public long addRomBatch(List<String> ecList, String userId, List<Double> weigh){
        long resultVal = 0;
        ReagentOutMasterModel model = new ReagentOutMasterModel();
        model.setBatchNo(DateUtil.format(new Date(),"yyyyMMddHHmmss"));
        model.setCreateTime(new Date());
        model.setRevokeState(1);// 1代表正常出库
        model.setUserId(userId);
        dao.insert(model);
        long romId = model.getId();
        int countVal = 0;
        for(int i = 0;i < ecList.size();i++){
            ReagentStock reagentStock = new ReagentStock();
            reagentStock.setEpcCode(ecList.get(i));
            reagentStock.setState("2");
            reagentStock.setLeftWeigh(weigh.get(i));
            if(reagentStockDao.updateByEpcCode(reagentStock) > 0){// 先更新仓库试剂状态为出库
                ReagentOutDetailModel detailModel = new ReagentOutDetailModel();
                detailModel.setCreateTime(new Date());
                detailModel.setEpcCode(ecList.get(i));
                detailModel.setCurrentWeigh(weigh.get(i));
                detailModel.setMasterId(romId);
                if(reagentOutDetailService.insert(detailModel) > 0){
                    countVal++;
                }
            }
        }
        if(ecList.size() == countVal){
            resultVal = romId;
        }
        return resultVal;
    }
}
